/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.sys.util.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CreationHelper;

/**
 *
 * @author Administrator
 */
public class ExportExcel {

    public static void exportExcel(Object[][] data, String sheetName) {
        HSSFWorkbook demoWorkBook = new HSSFWorkbook();
        HSSFSheet demoSheet = demoWorkBook.createSheet(sheetName);
        HSSFHeader header = demoSheet.getHeader();
        header.setCenter(sheetName);
        HSSFRow headerRow = demoSheet.createRow(0);
        for (int i = 0; i < data[0].length; i++) {
            HSSFCell headerCell = headerRow.createCell((short) i);
            //headerCell.setEncoding(HSSFCell.ENCODING_UTF_16);
            headerCell.setCellValue((String) data[0][i]);
        }
        for (int i = 1; i < data.length; i++) {
            createTableRow(demoWorkBook, demoSheet, data[i], i);
        }
        outPutExcel(demoWorkBook, demoSheet);
    }

    private static void createTableRow(HSSFWorkbook demoWorkBook, HSSFSheet demoSheet, Object[] line, int rowIndex) {
        //创建第rowIndex行
        //HSSFWorkbook workbook = new HSSFWorkbook();
        CreationHelper createHelper = demoWorkBook.getCreationHelper();
        HSSFCellStyle dstyle = demoWorkBook.createCellStyle();
        dstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));

        HSSFCell cell;
        HSSFRow row = demoSheet.createRow(rowIndex);
        for (int i = 0; i < line.length; i++) {

            Object aline = line[i] == null ? "" : line[i];
            cell = row.createCell(i);

            if (aline instanceof Double) {
                cell.setCellValue((Double) aline);
                cell.setCellStyle(dstyle);
                cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
            } else if (aline instanceof Integer) {
                cell.setCellValue((Integer) aline);
                cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
            } else {
                cell.setCellValue(aline.toString());
            }
        }
    }

    private static void outPutExcel(HSSFWorkbook demoWorkBook, HSSFSheet demoSheet) {
        try {
            String strfileName = "book.xls";
            FileOutputStream fos = null;
            //导出
            demoSheet.setGridsPrinted(true);//demoSheet
            HSSFFooter footer = demoSheet.getFooter();
            footer.setRight("Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages());
            // 获得JSF上下文环境
            FacesContext context = FacesContext.getCurrentInstance();
            // 获得ServletContext对象
            ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
            // 取得文件的绝对路径
            String excelName = servletContext.getRealPath("/upload") + "/" + strfileName;//strfileName
            File exportFile = new File(excelName);
            fos = new FileOutputStream(exportFile);//fos
            demoWorkBook.write(fos);//demoWorkBook
            fos.close();

            FacesContext ctx = FacesContext.getCurrentInstance();
            ctx.responseComplete();
            HttpServletResponse httpServletResponse = (HttpServletResponse) ctx.getExternalContext().getResponse();
            ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
            httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + strfileName);//strfileName
            httpServletResponse.setContentLength((int) exportFile.length());//exportFile
            httpServletResponse.setContentType("application/x-download");
            //httpServletResponse.setContentType("application/vnd.ms-excel");
            byte[] b = new byte[1024];
            int i = 0;
            FileInputStream fis = new java.io.FileInputStream(exportFile);
            while ((i = fis.read(b)) > 0) {
                servletOutputStream.write(b, 0, i);
            }
            servletOutputStream.flush();
            ctx.responseComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
